//
//  TableView_RQ_SPF_ZJQ.m
//  PlayBall
//
//  Created by Jdd‘s macbook on 13-10-29.
//
//

#import "Jdd_SXDS_TableView.h"

@interface Jdd_SXDS_TableView ()

@end

@implementation Jdd_SXDS_TableView
-(void)viewDidLoad
{
    [super viewDidLoad];
    //self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    UIView *v = [[UIView alloc] initWithFrame:CGRectMake(0,0,320,0)];
    self.tableView.tableFooterView = v;
    [self.tableView setSeparatorColor:SeparatorLineColor];
    if (IOS7) {
        [self.tableView setSeparatorInset:UIEdgeInsetsZero];
    }
    groupArray = [[NSMutableArray alloc] init];
    for (int i=0; i<[_tableData count]; ++i) {
        JddFollowNumber *follownum = [[JddFollowNumber alloc]init];
        follownum.open = 1;
        [groupArray addObject:follownum];
    }
}

-(void)sportTeamButtonHeadClicked:(NSNumber*)position{
    JddFollowNumber *groupDic = [groupArray objectAtIndex:[position intValue]];
    //设置groupDic中的展开状态
    if(groupDic.open ==0){
        groupDic.open=1;
    }else {
        groupDic.open = 0;
    }
    [self.tableView reloadSections:[NSIndexSet indexSetWithIndex:[position intValue]]
                  withRowAnimation:UITableViewRowAnimationFade];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return [_tableData count];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    NSDictionary* sectionCell = [_tableData objectAtIndex:section];
    NSArray *Matches = [sectionCell objectForKey:@"Matches"];
    if ([groupArray count]>0) {
        JddFollowNumber *groupDic = [groupArray objectAtIndex:section];
        if (groupDic.open==0) {
            return 0;
        }else {
            return [Matches count];
        }
        
    }else {
        return [Matches count];
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 70.0;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 0.0;//34.0;//modify by rain 隐藏group
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    JddPickNumSportItemCellHeader *cell = [JddPickNumSportItemCellHeader getInstanceView];
    NSDictionary* sectionCell = [_tableData objectAtIndex:section];
    NSArray *Matches = [sectionCell objectForKey:@"Matches"];
    NSDictionary *MatchCell = [Matches objectAtIndex:section];
//    NSString *Name = [sectionCell objectForKey:@"issue"];
    cell.day.text = @"";
    cell.Date.text = [JddCommonFunction convertWeekByNumber:[MatchCell objectForKey:@"Wk"]];
    cell.Mnum.text = [NSString stringWithFormat:@"共有%d场比赛可投", Matches.count];
    cell.statePosition = [NSNumber numberWithInt:section];
    cell.delegate = self;
    if ([groupArray count]>0) {
        JddFollowNumber *groupDic = [groupArray objectAtIndex:section];
        if (groupDic.open==0) {
            cell.HeadButton.image = [UIImage imageNamed:@"hemai_hall_down.png"];
        }else {
            cell.HeadButton.image = [UIImage imageNamed:@"hemai_up.png"];
        }
        
    }
    return cell;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    JddPickNumBDSXDSCell *cell = [JddPickNumBDSXDSCell getInstanceView];
    NSDictionary* sectionCell = [_tableData objectAtIndex:indexPath.section];
    NSArray *Match = [sectionCell objectForKey:@"Matches"];
    
    NSDictionary *MatchCell = [Match objectAtIndex:indexPath.row];
    NSString *ETime = [MatchCell objectForKey:@"ETime"];
    NSString *HTeam = [MatchCell objectForKey:@"HTeam"];
    NSString *MID = [MatchCell objectForKey:@"MID"];
    NSString *NMm = [MatchCell objectForKey:@"NMm"];
    NSString *VTeam = [MatchCell objectForKey:@"VTeam"];
    
    NSString *spStr = [MatchCell objectForKey:@"SpSXDS"];
    NSArray *spArr = [spStr componentsSeparatedByString:@"|"];
    if ([spArr count] == 4) {
        cell.sp1.text = [spArr objectAtIndex:0];
        cell.sp2.text = [spArr objectAtIndex:1];
        cell.sp3.text = [spArr objectAtIndex:2];
        cell.sp4.text = [spArr objectAtIndex:3];

    } else {
        cell.sp1.text = @"---";
        cell.sp2.text = @"---";
        cell.sp3.text = @"---";
        cell.sp4.text = @"---";
    }
    
    cell.weakNum.text = MID;
    cell.EndTime.text = [NSString stringWithFormat:@"%@截止",[ETime substringWithRange:NSMakeRange(11,5)]];
    cell.Match.text = NMm;
    cell.HTeam.text= HTeam;
    cell.VTeam.text = VTeam;
    
    cell.aSportNumber = [[JddSportNumber alloc] init];
    cell.aSportNumber.teamArray= [[NSMutableArray alloc] init];
    cell.aSportNumber.code = MID;
    cell.aSportNumber.Hteam = HTeam;
    cell.aSportNumber.Vteam = VTeam;
    cell.aSportNumber.endTime = ETime;
    
    for(JddSportNumber *num in self.sportlotterylist.sportList){
        if ([num.code isEqualToString:MID]) {
            for (NSNumber *num2 in num.teamArray) {
               UIButton *b = (UIButton *) [cell viewWithTag:[num2 intValue]];
                [b setSelected:YES];
            }
        }
    }
    
    cell.delegate = self;
    return cell;
}

-(void)sportTeamButtonClicked:(id)sender number:(JddSportNumber*) number1{
    [self.delegate performSelector:@selector(selectMatch:number:) withObject:sender withObject:number1];
}
@end
